package com.ruoyi.zhmk.service.impl;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.carApi.click.CarApiClick;
import com.ruoyi.carApi.click.impl.CarApiDefaultClick;
import com.ruoyi.carApi.entity.CarIdentification;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.zhmk.domain.ZhmkCar;
import com.ruoyi.zhmk.mapper.ZhmkCarMapper;
import com.ruoyi.zhmk.utils.CarApiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.zhmk.mapper.ZhmkCarinoutMapper;
import com.ruoyi.zhmk.domain.ZhmkCarinout;
import com.ruoyi.zhmk.service.IZhmkCarinoutService;
import com.ruoyi.common.core.text.Convert;

/**
 * 车辆进出记录Service业务层处理
 * 
 * @author sxl
 * @date 2021-06-15
 */
@Service
public class ZhmkCarinoutServiceImpl implements IZhmkCarinoutService 
{
    @Autowired
    private ZhmkCarinoutMapper zhmkCarinoutMapper;
    @Autowired
    private ZhmkCarMapper zhmkCarMapper;

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

    /**
     * 查询车辆进出记录
     * 
     * @param cinoutId 车辆进出记录ID
     * @return 车辆进出记录
     */
    @Override
    public ZhmkCarinout selectZhmkCarinoutById(Long cinoutId)
    {
        return zhmkCarinoutMapper.selectZhmkCarinoutById(cinoutId);
    }

    /**
     * 查询车辆进出记录列表
     * 
     * @param zhmkCarinout 车辆进出记录
     * @return 车辆进出记录
     */
    @Override
    public List<ZhmkCarinout> selectZhmkCarinoutList(ZhmkCarinout zhmkCarinout)
    {
        return zhmkCarinoutMapper.selectZhmkCarinoutList(zhmkCarinout);
    }

    /**
     * 新增车辆进出记录
     * 
     * @param zhmkCarinout 车辆进出记录
     * @return 结果
     */
    @Override
    public int insertZhmkCarinout(ZhmkCarinout zhmkCarinout)
    {
        zhmkCarinout.setCreateTime(DateUtils.getNowDate());
        return zhmkCarinoutMapper.insertZhmkCarinout(zhmkCarinout);
    }

    /**
     * 修改车辆进出记录
     * 
     * @param zhmkCarinout 车辆进出记录
     * @return 结果
     */
    @Override
    public int updateZhmkCarinout(ZhmkCarinout zhmkCarinout)
    {
        zhmkCarinout.setUpdateTime(DateUtils.getNowDate());
        return zhmkCarinoutMapper.updateZhmkCarinout(zhmkCarinout);
    }

    /**
     * 删除车辆进出记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteZhmkCarinoutByIds(String ids)
    {
        return zhmkCarinoutMapper.deleteZhmkCarinoutByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除车辆进出记录信息
     * 
     * @param cinoutId 车辆进出记录ID
     * @return 结果
     */
    @Override
    public int deleteZhmkCarinoutById(Long cinoutId)
    {
        return zhmkCarinoutMapper.deleteZhmkCarinoutById(cinoutId);
    }

    /**
     * 保存车辆记录
     * @param carIdentification
     * @return
     */
    @Override
    public int saveCarInfo(CarIdentification carIdentification) {
        String filePath = RuoYiConfig.getUploadPath();
        String fileName = System.currentTimeMillis()+"";
        String dateStr = simpleDateFormat.format(new Date());
        String filePathname = filePath+"/car/"+dateStr+"/"+fileName+".jpg";
        File file = new File(filePath+"/car/"+dateStr+"/");
        if(!file.exists()){
            file.mkdirs();
        }
        String webUrl = "/profile/upload/car/"+dateStr+"/"+fileName+".jpg";
        //保存车辆图片
        byte[] panoramaImageData = carIdentification.getPanoramaImageData();

        try {
            DataOutputStream out = new DataOutputStream(new FileOutputStream(filePathname));
            out.write(panoramaImageData);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //根据ip查询出设备信息
        String ip = carIdentification.getIp();
        ZhmkCar zhmkCar = zhmkCarMapper.selectZhmkCarByIp(ip);
        //进出类型
        String carInout = zhmkCar.getCarInout();
        //保存车辆进出记录表
        ZhmkCarinout zhmkCarinout = new ZhmkCarinout();
        zhmkCarinout.setCarName(carIdentification.getPlateNumber());
        zhmkCarinout.setCarInout(carInout);
        zhmkCarinout.setCarIp(ip);
        zhmkCarinout.setAvatar(webUrl);
        zhmkCarinout.setCreateTime(DateUtils.getNowDate());
        zhmkCarinout.setCinoutTime(DateUtils.getTime());
        zhmkCarinoutMapper.insertZhmkCarinout(zhmkCarinout);
        return 1;
    }
}
